######################################################################
# Time-stamp: <05/08/11 17:57:49 ostolop>
######################################################################
package EP::Common::Error;

use strict;
use base qw ( Error );

use EP::Common;
use EP::Common::DBI::Error;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;

  my %args;
  if ( @_ == 1 ) {
    $args{-text} = $_[0];
  } else {
    %args = @_;
  }

  my $error;

  if ( not exists $args{-text} ) {
    $error = EP::Common::DBI::Error->retrieve ( $args{-value} || UNKNOWN_ERROR );

    $args{-text} = sprintf( $error->message, @{ $args{-err_args} || [] } );
  }

  my $self = $class->SUPER::new(%args);
  $self->{-severity} = $error->severity if ref ( $error ) eq "EP::Common::DBI::Error";

  bless ($self, $class);
  return $self;
}

sub throw {
  my $self = shift;

  # we're two deep from Error.pm
  local $Error::Depth = $Error::Depth + 2;

  # if we are not rethrow-ing then create the object to throw
  $self = $self->new(@_) unless ref($self);

  die $Error::THROWN = $self;
}

sub severity {
  my $self = shift;
  exists $self->{'-severity'} ? $self->{'-severity'} : 0; # default severity 0 (INFO)
}

1;
